package service

import (
	"gin-vue-admin/global"
	"gin-vue-admin/model"
	"gin-vue-admin/model/request"
)

//@author: [piexlmax](https://github.com/piexlmax)
//@function: CreateUserOrderSendPic
//@description: 创建UserOrderSendPic记录
//@param: userOrderSendPic model.UserOrderSendPic
//@return: err error

func CreateUserOrderSendPic(userOrderSendPic model.UserOrderSendPic) (err error) {
	err = global.GVA_DB.Create(&userOrderSendPic).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteUserOrderSendPic
//@description: 删除UserOrderSendPic记录
//@param: userOrderSendPic model.UserOrderSendPic
//@return: err error

func DeleteUserOrderSendPic(userOrderSendPic model.UserOrderSendPic) (err error) {
	err = global.GVA_DB.Delete(&userOrderSendPic).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteUserOrderSendPicByIds
//@description: 批量删除UserOrderSendPic记录
//@param: ids request.IdsReq
//@return: err error

func DeleteUserOrderSendPicByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]model.UserOrderSendPic{},"id in ?",ids.Ids).Error
	return err
}

func DeleteUserOrderSendByOrderId(OrderId int) (err error) {
	err = global.GVA_DB.Delete(&[]model.UserOrderSendPic{},"order_id = ?",OrderId).Error
	return err
}
//@author: [piexlmax](https://github.com/piexlmax)
//@function: UpdateUserOrderSendPic
//@description: 更新UserOrderSendPic记录
//@param: userOrderSendPic *model.UserOrderSendPic
//@return: err error

func UpdateUserOrderSendPic(userOrderSendPic model.UserOrderSendPic) (err error) {
	err = global.GVA_DB.Save(&userOrderSendPic).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetUserOrderSendPic
//@description: 根据id获取UserOrderSendPic记录
//@param: id uint
//@return: err error, userOrderSendPic model.UserOrderSendPic

func GetUserOrderSendPic(id uint) (err error, userOrderSendPic model.UserOrderSendPic) {
	err = global.GVA_DB.Where("id = ?", id).First(&userOrderSendPic).Error
	return
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetUserOrderSendPicInfoList
//@description: 分页获取UserOrderSendPic记录
//@param: info request.UserOrderSendPicSearch
//@return: err error, list interface{}, total int64

func GetUserOrderSendPicInfoList(info request.UserOrderSendPicSearch) (err error, list interface{}, total int64) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&model.UserOrderSendPic{})
    var userOrderSendPics []model.UserOrderSendPic
    // 如果有条件搜索 下方会自动创建搜索语句
    if info.OrderId != 0 {
        db = db.Where("`order_id` = ?",info.OrderId)
    }
	err = db.Count(&total).Error
	err = db.Limit(limit).Offset(offset).Find(&userOrderSendPics).Error
	return err, userOrderSendPics, total
}